﻿using System;
using SurfaceAreaCalculationDomain;

namespace SurfaceAreaPresenter
{
    public enum Shape3DType { CUBE_TYPE, CONE_TYPE, CYLINDER_TYPE, SPHERE_TYPE };
    public class Presenter
    {
        private ISurfaceAreaView view;
        public ILogger logger;
        private Shape3DType shapeType;

        private Presenter() { }

        public Presenter(ISurfaceAreaView view, ILogger logger)
        {
            this.view = view;
            this.logger = logger;

            this.view.cubeButtonPressed += cubeButtonPressed;
            this.view.coneButtonPressed += coneButtonPressed;
            this.view.cylinderButtonPressed += cylinderButtonPressed;
            this.view.sphereButtonPressed += sphereButtonPressed;
            this.view.calculateButtonPressed += calculateButtonPressed;
            this.view.readLogButtonPressed += readLogButtonPressed;
            this.view.loggerStateChanged += loggerStateChanged;
        }

        private void setErrorControlsVisible(bool isVisible)
        {
            view.errorLabelVisible = isVisible;
        }

        private void setResultControlsVisible(bool isVisible)
        {
            view.resultButtonVisible = isVisible;
            view.resultTextFieldVisible = isVisible;
        }

        private void setEdgeControlsVisible(bool isVisible)
        {
            view.edgeLabelVisible = isVisible;
            view.edgeTextFieldVisible = isVisible;
        }

        private void setRadiusControlsVisible(bool isVisible)
        {
            view.radiusLabelVisible = isVisible;
            view.radiusTextFieldVisible = isVisible;
        }

        private void setDirectionControlsVisible(bool isVisible)
        {
            view.directionLabelVisible = isVisible;
            view.directionTextFieldVisible = isVisible;
        }

        private void cubeButtonPressed()
        {
            logger.write("cubeButtonPressed;");
            this.shapeType = Shape3DType.CUBE_TYPE;
            this.setEdgeControlsVisible(true);
            this.setRadiusControlsVisible(false);
            this.setDirectionControlsVisible(false);
            this.setResultControlsVisible(true);
            this.setErrorControlsVisible(false);
        }

        private void coneButtonPressed()
        {
            logger.write("coneButtonPressed;");
            this.shapeType = Shape3DType.CONE_TYPE;
            this.setEdgeControlsVisible(false);
            this.setRadiusControlsVisible(true);
            this.setDirectionControlsVisible(true);
            this.setResultControlsVisible(true);
            this.setErrorControlsVisible(false);
        }

        private void cylinderButtonPressed()
        {
            logger.write("cylinderButtonPressed;");
            this.shapeType = Shape3DType.CYLINDER_TYPE;
            this.setEdgeControlsVisible(false);
            this.setRadiusControlsVisible(true);
            this.setDirectionControlsVisible(true);
            this.setResultControlsVisible(true);
            this.setErrorControlsVisible(false);
        }

        private void sphereButtonPressed()
        {
            logger.write("sphereButtonPressed;");
            this.shapeType = Shape3DType.SPHERE_TYPE;
            this.setEdgeControlsVisible(false);
            this.setRadiusControlsVisible(true);
            this.setDirectionControlsVisible(false);
            this.setResultControlsVisible(true);
            this.setErrorControlsVisible(false);
        }

        private void calculateButtonPressed()
        {
            logger.write("calculateButtonPressed;");
            try
            {
                this.calculateSurfaceArea();
            }
            catch (Exception ex)
            {
                view.errorValue = "Please set all parameters correctly";
                this.setErrorControlsVisible(true);
                return;
            }
        }

        private void loggerStateChanged(bool state)
        {
            if (state == true)
            {
                logger = view.getFileLogger();
            }
            else
            {
                logger = view.getHTMLLogger();
            }
        }

        private void readLogButtonPressed()
        {
            logger.write("readLogButtonPressed;");
            view.logRichTextBoxVisible = true;
            view.logText = logger.read();
        }

        public void calculateSurfaceArea()
        {
            IShape3D shapeObject = null;
            double edgeValue = 0, radiusValue = 0, directionValue = 0;

            switch (shapeType)
            {
                case Shape3DType.CUBE_TYPE:
                    edgeValue = System.Convert.ToDouble(view.edgeValue);
                    shapeObject = new Cube { edge = edgeValue };
                    break;
                case Shape3DType.CONE_TYPE:
                    radiusValue = System.Convert.ToDouble(view.radiusValue);
                    directionValue = System.Convert.ToDouble(view.directionValue);
                    shapeObject = new Cone { radius = radiusValue, directing = directionValue };
                    break;
                case Shape3DType.CYLINDER_TYPE:
                    radiusValue = System.Convert.ToDouble(view.radiusValue);
                    directionValue = System.Convert.ToDouble(view.directionValue);
                    shapeObject = new Cylinder { radius = radiusValue, directing = directionValue };
                    break;
                case Shape3DType.SPHERE_TYPE:
                    radiusValue = System.Convert.ToDouble(view.radiusValue);
                    shapeObject = new Sphere { radius = radiusValue };
                    break;
            }
            this.setErrorControlsVisible(false);
            view.resultField = System.Convert.ToString(shapeObject.computeSurfaceArea());
        }
    }
}
